package com.sunshine.lnuplus.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.adapter.ChooseWeekRecyclerAdapter
import com.sunshine.lnuplus.model.LessonBean
import com.sunshine.lnuplus.model.LessonStyle
import com.sunshine.lnuplus.utils.ColorUtils

/**
 * Created by sunshine on 2020/4/14
 */
class CalenderLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr){

    private var dayCount: Int = 5
    private var thisWeek = 1
    private var otherWeek = 1
    private var lastLessonLayoutData: LastLessonLayoutData? = null
    private lateinit var layoutMap: MutableMap<Int, FrameLayout>
    private lateinit var dateLayoutMap: MutableMap<Int, LinearLayout>
    private lateinit var lessonStyle: LessonStyle
    private lateinit var lessonList: ArrayList<LessonBean>
    private lateinit var allLessonList: ArrayList<LessonBean>   //无论是否显示非本周，这个表中都是全的
    private var fragmentManager: FragmentManager? = null
    private var isSetting = false   //设置界面不相应点击事件
    private var fragment: Fragment? = null
    private var adapter: ChooseWeekRecyclerAdapter? = null

    fun init(
        lessonList: ArrayList<LessonBean>,
        lessonStyle: LessonStyle,
        thisWeek: Int,
        otherWeek: Int,
        fragmentManager: FragmentManager?,
        isSetting: Boolean,
        fragment: Fragment?,
        adapter: ChooseWeekRecyclerAdapter?
    ) {
        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.layout_calender, this)

        ColorUtils.init(context)
        this.lessonStyle = lessonStyle
        this.lessonList = lessonList
        this.allLessonList = lessonList
        this.thisWeek = thisWeek
        this.otherWeek = otherWeek
        this.fragmentManager = fragmentManager
        this.isSetting = isSetting
        this.fragment = fragment
        this.adapter = adapter
        lastLessonLayoutData = null

        //夜间模式设置背景
        if(context.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
            this.lessonStyle.dateAndTimeTextColor = "#FFFFFF"
            this.lessonStyle.alpha = 100.coerceAtMost(this.lessonStyle.alpha)
        }

        lessonList.forEach {
            if(it.end > lessonStyle.count){
                lessonStyle.count = it.end
            }
        }
    }
}